package com.os.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.server.SocketSecurityException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.os.service.SubmitdeskServiceX;
import com.os.serviceimpl.SubDeskServiceImp;

import net.sf.json.JSONObject;

public class SubmitFoodsControllerX extends HttpServlet{
	
	SubmitdeskServiceX sub = new SubDeskServiceImp();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		String fids = req.getParameter("fids");
		String fnums = req.getParameter("fnums");
		byte [] newFnums = fnums.getBytes();//菜单份数
		String [] fidss = fids.split("fid=");
		StringBuffer fs =new StringBuffer();//菜名
		for (int i = 0; i < fidss.length; i++) {
			fs.append(fidss[i]);
		}
		String newFidss = fs.toString();
		StringBuffer fss =new StringBuffer();//菜名
		String [] newFids = newFidss.split("&");//菜单id
		for (int i = 0; i < newFids.length; i++) {
			int num = Integer.parseInt(String.valueOf((char)newFnums[i]));
			for (int j = 0; j < num; j++) {
				if(!(i==newFids.length-1&&j==num-1)){
					fss.append(newFids[i]);
					fss.append(",");
				}else{
					fss.append(newFids[i]);
				}
			}
		}
		System.out.println(fss.toString());
		String pcounts = req.getParameter("pcount");
		int pcount = Integer.parseInt(pcounts);
		/*System.out.println("人数"+pcount);*/
	    Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=format.format(date);//当前时间
		/*System.out.println("时间"+time);*/
		HttpSession session =  req.getSession();
		String dids =  req.getParameter("did");//桌子的id
		int did = Integer.parseInt(dids);
		int uid =(int) session.getAttribute("uid");//服务员id
		boolean bl = sub.submitDesk(fss.toString(), uid, did, time, pcount,-1);
		JSONObject obj = new JSONObject();
		obj.put("boole", bl);
		PrintWriter pw = resp.getWriter();
		pw.write(obj.toString());
		pw.flush(); pw.close();
		}
	}

